<?php

//decode by http://www.yunlu99.com/
require MODULE_ROOT . "/helpers/functions.php";
global $_GPC, $_W;
$op = $_GPC["op"] ?: "index";
$user = user();
$config = config();
$uniacid = uniacid();
$uid = uid();
$group = $user["group"] ? get_group_info_by_id($user["group"]) : false;
check_auth();
if ($op == "index") {
	$where = array("uniacid = {$uniacid}");
	$sql = "SELECT * FROM " . tablename("d1sj_card_category") . " WHERE " . implode(" AND ", $where);
	$categories = pdo_fetchall($sql);
	$res = array();
	foreach ($categories as $category) {
		$tmp = array("id" => $category["id"], "name" => $category["name"], "money_min" => figure_money($category["money_min"]), "money_max" => figure_money($category["money_max"]));
		$res[] = $tmp;
	}
	$categories = json_encode($res, JSON_UNESCAPED_UNICODE);
	$banners = get_banner_by_type("lend");
	include $this->template("lend");
} else {
	if ($op == "info") {
		$id = input("id", 0);
		$sql = "SELECT * FROM " . tablename("d1sj_card_lend") . " WHERE id = :id";
		$info = pdo_fetch($sql, array(":id" => $id));
		if ($info["day_term_star"]) {
			$periodRange = range($info["day_term_star"], $info["day_term_end"]);
			$periodType = "日";
		} else {
			$periodRange = range($info["month_term_star"], $info["month_term_end"]);
			$periodType = "月";
		}
		if ($info["daily_interest_rate"]) {
			$dailyInterestType = "日";
		} else {
			$dailyInterestType = "月";
		}
		$periodRange = json_encode($periodRange);
		include $this->template("lend_info");
	} else {
		if ($op == "getData") {
			$type = input("type");
			$category = input("category");
			$where = array("uniacid = {$uniacid}");
			if ($category) {
				array_push($where, "category_id = {$category}");
			}
			if ($type == "hot") {
				array_push($where, "recommend = 1");
			} else {
				if ($type == "new") {
					array_push($where, "is_new = 1");
				} else {
					$order = " order by sort asc";
				}
			}
			$sql = "SELECT * FROM " . tablename("d1sj_card_lend") . " WHERE " . implode(" AND ", $where) . $order;
			$lends = pdo_fetchall($sql);
			$res = array();
			foreach ($lends as $lend) {
				$tmp = array("icon" => tomedia($lend["icon"]), "title" => $lend["title"], "apply_num" => $lend["application_number"] + $lend["true_click"], "rate" => $lend["daily_interest_rate"] ?: $lend["annual_interest_rate"], "rate_type" => $lend["daily_interest_rate"] ? "日" : "月", "quota_min" => (int) $lend["quota_min"], "quota_max" => (int) $lend["quota_max"], "success_rate" => $lend["success_rate"], "traits" => explode(",", $lend["trait"]), "url" => $this->createMobileUrl("lend", array("id" => $lend["id"], "op" => "info")));
				$res[] = $tmp;
			}
			return_json($res);
		} else {
			if ($op == "submit") {
				$user_sql = "SELECT * FROM " . tablename("d1sj_card_user") . " WHERE uid = :uid";
				$user_list = pdo_fetch($user_sql, array(":uid" => $uid));
				if (!$user_list["mobile"]) {
					message("请先填写资料", $this->createMobileUrl("user"), "error");
				}
				$cid = input("id");
				$sql = "SELECT * FROM " . tablename("d1sj_card_lend") . " WHERE id = :id";
				$bank = pdo_fetch($sql, array(":id" => $cid));
				$input = input("data");
				$name = $_GPC["data"]["name"];
				$mobile = $_GPC["data"]["mobile"];
				$data = array("uniacid" => $uniacid, "uid" => $uid, "bank" => $bank["id"], "name" => $input["name"], "mobile" => $input["mobile"], "createtime" => time(), "type" => 2);
				$rates = explode("|", $config["rate"]);
				$bankMoney = $bank["price"];
				$data["fan_total"] = $bankMoney;
				if ($user["leader1"]) {
					$group1 = get_group_info_by_id($user["leader1"], true);
					$groupLevel1 = $group1 ? $group1["mlevel"] : 0;
				}
				if ($user["leader2"]) {
					$group2 = get_group_info_by_id($user["leader2"], true);
					$groupLevel2 = $group2 ? $group2["mlevel"] : 0;
				}
				if ($user["leader3"]) {
					$group3 = get_group_info_by_id($user["leader3"], true);
					$groupLevel3 = $group3 ? $group3["mlevel"] : 0;
				}
				if ($user["leader1"] && $rates[0] && $groupLevel1 >= 1) {
					$data["fan_uid1"] = $user["leader1"];
					$data["fan_level1"] = $bankMoney * ($rates[0] / 100);
				}
				if ($user["leader2"] && $rates[1] && $groupLevel2 >= 2) {
					$data["fan_uid2"] = $user["leader2"];
					$data["fan_level2"] = $bankMoney * ($rates[1] / 100);
				}
				if ($user["leader3"] && $rates[2] && $groupLevel3 >= 3) {
					$data["fan_uid3"] = $user["leader3"];
					$data["fan_level3"] = $bankMoney * ($rates[2] / 100);
				}
				$res = pdo_insert("d1sj_card_record", $data);
				pdo_update("d1sj_card_lend", array("true_click" => $bank["true_click"] + 1), array("id" => $bank["id"]));
				header("Location: " . $bank["url"]);
			}
		}
	}
}